package com.example.goods.service.impl;

import com.example.goods.bean.Cart;
import com.example.goods.bean.ProductInfo;
import com.example.goods.common.ResultResponse;
import com.example.goods.repository.CartRepository;
import com.example.goods.repository.ProductInfoRepository;
import com.example.goods.service.CartService;
import com.example.goods.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Optional;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/7/5
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartRepository cartRepository;

    @Autowired
    private ProductInfoRepository productInfoRepository;

    /**
     * 添加购物车
     *
     * @param userId
     * @param productId
     * @param count
     * @return
     */
    @Override
    public ResultResponse<Cart> addCart(Integer userId, String productId, Integer count) {
        BigDecimal bigDecimal = new BigDecimal(BigInteger.ZERO);
        Cart cart = cartRepository.findByUserIdAndProductId(userId, productId);
        Optional<ProductInfo> productInfo = productInfoRepository.findById(productId);
        if (cart == null) {
            //该商品不在购物车中则添加商品到购物车
            if (productInfo.isPresent()) {
                Cart items = new Cart();
                items.setQuantity(count);
                items.setChecked(1);
                items.setPrice(productInfo.get().getProductPrice());
                items.setProductId(productInfo.get().getProductId());
                items.setUserId(userId);
                items.setQuantity(count);
                bigDecimal = productInfo.get().getProductPrice().multiply(new BigDecimal(items.getQuantity())).add(bigDecimal);
                items.setOderAmount(bigDecimal);
                items.setProductIcon(productInfo.get().getProductIcon());
                items.setProductName(productInfo.get().getProductName());
                return ResultResponse.createBySuccessMessage("添加购物车成功", cartRepository.save(items));
            }

        } else {
            //商品已在购物车中则数量相加
            count = cart.getQuantity() + count;
            cart.setQuantity(count);
            bigDecimal = productInfo.get().getProductPrice().multiply(new BigDecimal(cart.getQuantity())).add(bigDecimal);
            cart.setOderAmount(bigDecimal);
            return ResultResponse.createBySuccess(cartRepository.save(cart));
        }
        return ResultResponse.createByErrorMessage("参数错误");
    }

    /**
     * 查询购物车中商品
     *
     * @param id
     * @return
     */
    @Override
    public ResultResponse<Optional<Cart>> select(Integer id) {
        Optional<Cart> cart = cartRepository.findById(id);
        if (cart.isPresent()) {
            return ResultResponse.createBySuccess(cart);
        }
        return ResultResponse.createByErrorMessage("该商品部在购物车之中");

    }


    /**
     * 更新购物车商品数量
     */
    @Override
    public ResultResponse<Cart> update(Integer userId,String productId,Integer quantity){
        BigDecimal bigDecimal = new BigDecimal(BigInteger.ZERO);
        Optional<ProductInfo> productInfo = productInfoRepository.findById(productId);
        Cart cart = cartRepository.findByUserIdAndProductId(userId, productId);
        if (productInfo.isPresent()){
            cart.setQuantity(quantity);
            bigDecimal = productInfo.get().getProductPrice().multiply(new BigDecimal(cart.getQuantity())).add(bigDecimal);
            cart.setOderAmount(bigDecimal);
            return ResultResponse.createBySuccessMessage("更新成功",cartRepository.save(cart));
        }
        return ResultResponse.createByErrorMessage("更新失败");
    }

    /**
     * 购物车列表
     * @return
     */
    @Override
    public ResultResponse<CartVO> findList(){
        BigDecimal bigDecimal = new BigDecimal(BigInteger.ZERO);
        CartVO cartVO = new CartVO();
        List<Cart> list = cartRepository.findAll();
        if (!list.isEmpty()){
            for (Cart cart:list){
                if (cart.getChecked()!=0){
                    bigDecimal=cart.getOderAmount().add(bigDecimal);
                }
                cartVO.setCartTotalPrice(bigDecimal);
            }
            cartVO.setCartProductVoList(list);
        }
        return ResultResponse.createBySuccess(cartVO);
    }

    /**
     * 删除购物车商品
     * @param id
     */
    @Override
    public void deleteCart(Integer id) {
        cartRepository.deleteById(id);
    }

    @Override
    public ResultResponse<Cart> checked(Integer userId,String productId,Integer checked){
            Cart cart = cartRepository.findByProductId(productId);
            if (cart==null){
                return ResultResponse.createByErrorMessage("商品不存在");
            }
            cart.setChecked(checked);
            return ResultResponse.createBySuccess(cartRepository.save(cart));
    }

}
